using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;
using System.Data.Linq.SqlClient;

namespace VoiceAd.DAL
{
    public class BroadCastRepository : IBroadCastRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public BroadCastRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblBroadCast> GetListBroadCasts()
        {
            return context.tblBroadCasts.ToList();
        }

        public IEnumerable<tblBroadCast> GetListSearchBroadCasts(string StationName)
        {
            IEnumerable<tblBroadCast> lst = from a in context.tblBroadCasts
                                            where SqlMethods.Like(a.tblStation.StationName, "%" + StationName + "%")
                                            select a;

            return lst;
        }

        public tblBroadCast GetBroadCastById(int id)
        {
            return context.tblBroadCasts.Where(x => x.Id == id).FirstOrDefault();
        }

        public void InsBroadCast(tblBroadCast tblbroadcast)
        {
            context.tblBroadCasts.InsertOnSubmit(tblbroadcast);
            context.SubmitChanges();
        }

        public void UpdBroadCast(tblBroadCast tblbroadcast)
        {
            tblBroadCast obj = context.tblBroadCasts.Where(x => x.Id == tblbroadcast.Id).First();
            obj.StationId = tblbroadcast.StationId;
            obj.SectionId = tblbroadcast.SectionId;
            obj.BroadCastTime = tblbroadcast.BroadCastTime;
            obj.Price = tblbroadcast.Price;
            context.SubmitChanges();
        }

        public void DelBroadCast(int id)
        {
            tblBroadCast obj = context.tblBroadCasts.Single(x => x.Id == id);
            context.tblBroadCasts.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
